package pl.edu.mimuw.irs.rcp.view.navigation;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import pl.edu.mimuw.irs.rcp.persistence.IExternalResource;
import pl.edu.mimuw.irs.rcp.persistence.PersistenceManager;
import pl.edu.mimuw.irs.rcp.persistence.descriptor.HistoryFilesDescriptor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

public class NavigationViewContentProvider implements IStructuredContentProvider, 
		ITreeContentProvider {
	
    public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	}
    
	public void dispose() {
	}
    
	public Object[] getElements(Object parent) {
		return getChildren(parent);
	}
    
	public Object getParent(Object child) {
		if (child instanceof IResource) {
			return ((IResource) child).getParent();
		} else if (child instanceof IExternalResource) {
			return ((IExternalResource) child).getParent();
		} else {
			return null;
		}
	}
    
	public Object[] getChildren(Object parent) {
		
		if (parent instanceof IWorkspaceRoot) {
			IWorkspaceRoot rootWorkspace = (IWorkspaceRoot)parent;
			return rootWorkspace.getProjects();
		} else if (parent instanceof IProject) {
			IProject project = (IProject)parent;
			try {
				if (project.isOpen()) {
					/* Create first element as reference to history files */
					List<Object> members = new LinkedList<Object>();
					HistoryFilesDescriptor descriptor = PersistenceManager.getInstance(project).getHistoryDescriptor();
					members.add(descriptor);
					for (Object member : project.members()) {
						if (member instanceof IResource) {
							if (!((IResource) member).getName().startsWith(".")) {
								members.add(member);
							}
						}
					}
					return members.toArray();
				}
				else return new Object[0];
			} catch (CoreException e) {
				Logger.handle(MessageCode.PROJECT_OPEN_PROBLEM, e, project.getName());
				return new Object[0];
			}
		} else if (parent instanceof HistoryFilesDescriptor) {
			HistoryFilesDescriptor external = (HistoryFilesDescriptor)parent;
			try {
				return external.getChildren();
			} catch (Exception e) {
				Logger.handle(MessageCode.HISTORY_LOAD_DESCRIPTOR_PROBLEM, e);
				return new Object[0];
			}
		} else {
			return new Object[0];
		}
		
	}

    public boolean hasChildren(Object parent) {
    	if (parent instanceof IWorkspaceRoot) {
			IWorkspaceRoot rootWorkspace = (IWorkspaceRoot)parent;
			return rootWorkspace.getProjects().length > 0;
		} else if (parent instanceof IProject) {
			IProject project = (IProject)parent;
			try {
				if (project.isOpen()) return project.members().length > 0;
				else return false;
			} catch (CoreException e) {
				Logger.handle(MessageCode.PROJECT_OPEN_PROBLEM, e, project.getName());
				return false;
			}
		} else if (parent instanceof HistoryFilesDescriptor) {
			return true;
		} else {
			return false;
		}
	}
}

